﻿package plugin.active
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import uiresource.active.*;
    import view.component.*;
    import view.mornExtends.*;
    import vo.*;

    public class ActiveUICtrl extends ActiveUI
    {
        private var _curData:Object;
        private var user_level:int;
        private var _taskgonfig:Object;
        private var tasklistArr:Array;
        private var taskcount:int = 0;
        private var awardgobfig:Object;
        private var _cfgItem:Object;
        private var _instance:Active;

        public function ActiveUICtrl(param1:Active)
        {
            this._taskgonfig = ConfigDictionary.data.active;
            this.tasklistArr = [];
            this.awardgobfig = ConfigDictionary.data.activerewards;
            this._cfgItem = ConfigDictionary.data.item;
            this._instance = param1;
            btn_close.clickHandler = new Handler(this._instance.close);
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.updatelevel();
            this.initSet();
            return;
        }// end function

        override protected function createChildren() : void
        {
            loadUI("active/Active.xml");
            return;
        }// end function

        private function initSet() : void
        {
            var _loc_1:* = Control.create(ActiveControl, this);
            ModelProxy.addObserver("active", _loc_1);
            return;
        }// end function

        public function showMsg(param1:Object) : void
        {
            this._curData = param1;
            this.tasklist();
            this.showBonus();
            this.showGoods();
            return;
        }// end function

        private function showGoods() : void
        {
            list_goods.array = this.awardgobfig as Array;
            list_goods.renderHandler = new Handler(this.goodsRender);
            return;
        }// end function

        private function goodsRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:List = null;
            var _loc_7:List = null;
            var _loc_8:Array = null;
            var _loc_9:Button = null;
            var _loc_10:Array = null;
            var _loc_11:List = null;
            var _loc_12:Array = null;
            var _loc_13:String = null;
            var _loc_14:Array = null;
            var _loc_15:Object = null;
            if (param2 < list_goods.length)
            {
                _loc_3 = list_goods.array[param2];
                _loc_4 = this._curData.bonus;
                _loc_5 = this._curData[_loc_3.rewardid];
                _loc_6 = param1.getChildByName("txt_num") as List;
                _loc_7 = param1.getChildByName("txt_num") as List;
                _loc_8 = this.fenjiarr(list_goods.array[param2].bonus);
                _loc_6.array = _loc_8;
                _loc_9 = param1.getChildByName("btn_qd") as Button;
                if (_loc_5 == 1)
                {
                    _loc_9.label = "已领取";
                    _loc_9.disabled = true;
                    _loc_9.visible = true;
                    _loc_9.mouseChildren = false;
                    _loc_9.mouseEnabled = false;
                }
                else if (_loc_4 >= _loc_3.bonus)
                {
                    _loc_9.label = "领取";
                    _loc_9.disabled = false;
                    _loc_9.visible = true;
                    _loc_9.mouseChildren = true;
                    _loc_9.mouseEnabled = true;
                    _loc_9.clickHandler = new Handler(this.doLq, [_loc_3.rewardid]);
                }
                else
                {
                    _loc_9.label = "领取";
                    _loc_9.disabled = true;
                    _loc_9.visible = true;
                    _loc_9.mouseChildren = false;
                    _loc_9.mouseEnabled = false;
                }
                _loc_10 = [];
                _loc_11 = param1.getChildByName("list_item") as List;
                _loc_12 = list_goods.array[param2].reward.split("&");
                for each (_loc_13 in _loc_12)
                {
                    
                    _loc_14 = _loc_13.split("#");
                    _loc_15 = ItemObject.create(this._cfgItem[_loc_14[0]]);
                    _loc_15.count = _loc_14[1];
                    _loc_10.push(_loc_15);
                }
                _loc_11.array = _loc_10;
                _loc_11.renderHandler = new Handler(this.itemRender, [_loc_11]);
            }
            return;
        }// end function

        private function fenjiarr(param1:int) : Array
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:String = null;
            var _loc_9:int = 0;
            var _loc_2:Array = [];
            var _loc_3:Array = [];
            if (param1 < 100)
            {
                _loc_4 = param1 % 10;
                _loc_5 = Math.floor(param1 / 10);
                _loc_7 = _loc_5 + "," + _loc_4;
                _loc_2 = _loc_7.split(",");
            }
            if (param1 >= 100 && param1 < 1000)
            {
                _loc_4 = param1 % 10;
                _loc_9 = param1 % 100;
                _loc_5 = Math.floor(_loc_9 / 10);
                _loc_6 = Math.floor(param1 / 100);
                _loc_7 = _loc_6 + "," + _loc_5 + "," + _loc_4;
                _loc_2 = _loc_7.split(",");
            }
            var _loc_8:int = 0;
            while (_loc_8 < _loc_2.length)
            {
                
                _loc_3.push({num:"png.common." + _loc_2[_loc_8]});
                _loc_8++;
            }
            return _loc_3;
        }// end function

        private function itemRender(param1:List, param2:Component, param3:int) : void
        {
            if (param3 < param1.length)
            {
                (param2 as FlashItem).effshow = true;
                (param2 as FlashItem).data = param1.array[param3];
            }
            return;
        }// end function

        private function doLq(param1:int) : void
        {
            var _loc_2:* = UserData.getBagData();
            var _loc_3:* = list_msg.localToGlobal(new Point(0, 0));
            ModelProxy.change("data", "itempoint", _loc_3);
            if (_loc_2.emptycount == 0)
            {
                ModelProxy.change("movetips", "error", "背包空间不足，请先整理背包");
            }
            else
            {
                PluginManager.instance.sendToServer("active", CMDActive.GetReward, {rid:this._curData.rid, rewardid:param1});
            }
            return;
        }// end function

        public function showBonus() : void
        {
            var _loc_1:* = this._curData.bonus;
            if (_loc_1 < 10)
            {
                list.array = [{num:"png.common." + _loc_1}];
            }
            else
            {
                list.array = this.fenjiarr(_loc_1);
            }
            txt_value.visible = false;
            pro_bar.width = _loc_1 / 140 * 321;
            return;
        }// end function

        public function updatelevel() : void
        {
            var _loc_1:* = ModelProxy.getValue("data", "user");
            this.user_level = _loc_1.level;
            return;
        }// end function

        private function tasklist() : void
        {
            var _loc_2:Object = null;
            var _loc_3:Boolean = false;
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:int = 0;
            this.tasklistArr = [];
            var _loc_1:int = 0;
            for each (_loc_4 in this._taskgonfig)
            {
                
                _loc_2 = _loc_4;
                if (this.user_level >= _loc_4.level && _loc_4.show == 1)
                {
                    this.taskcount = this._curData[_loc_2.acid];
                    _loc_3 = false;
                    _loc_3 = this.taskcount == _loc_2.count;
                    if (_loc_3 && _loc_2.type != 0)
                    {
                        _loc_1 = _loc_1 + 1;
                        _loc_2.shunxu = this._taskgonfig.length + _loc_1;
                        this.tasklistArr.push(_loc_2);
                    }
                    if (_loc_3 && _loc_2.type == 0)
                    {
                        _loc_2.shunxu = _loc_2.index;
                        this.tasklistArr.push(_loc_2);
                    }
                }
            }
            _loc_5 = 0;
            for each (_loc_6 in this._taskgonfig)
            {
                
                _loc_2 = _loc_6;
                if (this.user_level >= _loc_4.level && _loc_4.show == 1)
                {
                    _loc_7 = this._curData[_loc_2.acid];
                    _loc_3 = false;
                    _loc_3 = _loc_7 == _loc_2.count;
                    if (_loc_3 == false && _loc_2.type != 0)
                    {
                        _loc_5 = _loc_5 + 1;
                        _loc_2.shunxu = _loc_5;
                        this.tasklistArr.push(_loc_2);
                    }
                    if (_loc_3 == false && _loc_2.type == 0)
                    {
                        _loc_2.shunxu = _loc_2.index;
                        this.tasklistArr.push(_loc_2);
                    }
                }
            }
            this.tasklistArr.sortOn("shunxu", Array.NUMERIC);
            list_msg.array = this.tasklistArr;
            list_msg.renderHandler = new Handler(this.listRender);
            return;
        }// end function

        private function listRender(param1:Component, param2:int) : void
        {
            var _loc_3:Image = null;
            var _loc_4:Boolean = false;
            var _loc_5:Object = null;
            var _loc_6:Label = null;
            var _loc_7:Label = null;
            var _loc_8:Label = null;
            var _loc_9:Label = null;
            var _loc_10:LinkButton = null;
            var _loc_11:Image = null;
            if (param2 < list_msg.length)
            {
                _loc_3 = param1.getChildByName("imgtx") as Image;
                _loc_4 = false;
                _loc_5 = list_msg.array[param2];
                _loc_6 = param1.getChildByName("txt_msg") as Label;
                _loc_6.text = _loc_5.name;
                _loc_7 = param1.getChildByName("txt_desc") as Label;
                this.taskcount = this._curData[_loc_5.acid];
                _loc_7.text = this.taskcount + "/" + _loc_5.count;
                _loc_8 = param1.getChildByName("txt_sx") as Label;
                _loc_8.text = _loc_5.bonus;
                _loc_9 = param1.getChildByName("txt_link") as Label;
                _loc_10 = param1.getChildByName("txt_go") as LinkButton;
                _loc_10.label = "立即前往";
                _loc_11 = param1.getChildByName("blueline") as Image;
                _loc_11.url = "png.common.cs2_selectbg" + (param2 % 2 + 1);
                switch(int(_loc_5.type))
                {
                    case 0:
                    {
                        _loc_10.visible = false;
                        break;
                    }
                    case 4:
                    {
                        if (this.qiandao())
                        {
                            _loc_10.visible = false;
                        }
                        else
                        {
                            _loc_10.visible = true;
                        }
                        break;
                    }
                    default:
                    {
                        _loc_10.visible = true;
                        break;
                        break;
                    }
                }
                _loc_4 = this.taskcount == _loc_5.count;
                if (_loc_4)
                {
                    _loc_9.text = "已完成";
                    _loc_10.visible = false;
                    _loc_9.color = MColor.New62;
                }
                else
                {
                    _loc_9.text = "未完成";
                    _loc_9.color = MColor.New56;
                }
                _loc_10.removeEventListener(MouseEvent.CLICK, this.txtgo);
                _loc_10.addEventListener(MouseEvent.CLICK, this.txtgo);
                _loc_6.removeEventListener(MouseEvent.MOUSE_OVER, this.showTips);
                _loc_6.addEventListener(MouseEvent.MOUSE_OVER, this.showTips);
                param1.removeEventListener(MouseEvent.MOUSE_OVER, this.onitemOver);
                param1.removeEventListener(MouseEvent.MOUSE_OUT, this.OnBooimg);
                param1.addEventListener(MouseEvent.MOUSE_OVER, this.onitemOver);
                param1.addEventListener(MouseEvent.MOUSE_OUT, this.OnBooimg);
            }
            return;
        }// end function

        protected function onitemOver(event:MouseEvent) : void
        {
            (event.currentTarget.getChildByName("imgtx") as Image).visible = true;
            (event.currentTarget.getChildByName("imgtx") as Image).mouseEnabled = false;
            return;
        }// end function

        private function OnBooimg(event:MouseEvent) : void
        {
            (event.currentTarget.getChildByName("imgtx") as Image).visible = false;
            return;
        }// end function

        protected function txtgo(event:MouseEvent) : void
        {
            var _loc_4:Array = null;
            var _loc_5:Point = null;
            var _loc_6:Array = null;
            var _loc_7:Array = null;
            var _loc_2:* = ((event.currentTarget as LinkButton).parent as Component).dataSource.time;
            if (_loc_2)
            {
                if (this.isOpenActive(_loc_2) == false)
                {
                    ModelProxy.change("movetips", "error", "活动尚未开启，禁止传送");
                    return;
                }
            }
            var _loc_3:* = ((event.currentTarget as LinkButton).parent as Component).dataSource.type + "," + ((event.currentTarget as LinkButton).parent as Component).dataSource.target;
            _loc_4 = _loc_3.split(",");
            switch(int(_loc_4[0]))
            {
                case 0:
                {
                    break;
                }
                case 1:
                case 3:
                case 4:
                {
                    if (_loc_4[1] != "")
                    {
                        _loc_7 = _loc_4[1].split("#");
                        if (_loc_7[0])
                        {
                            if (_loc_7[0] == "uniondonation")
                            {
                                if (UserData.getUserData("unionId") == 0)
                                {
                                    ModelProxy.change("movetips", "error", "您没有行会，请先加入行会！");
                                    return;
                                }
                            }
                            if (_loc_7[0] == "xunbao" || _loc_7[0] == "juezhan" || _loc_7[0] == "instance" || _loc_7[0] == "meirishouchong" || _loc_7[0] == "gameactive")
                            {
                                _loc_5 = UIGlobel.pointsDic[_loc_7[0]];
                            }
                            else if (_loc_7[0] == "zbhuishou")
                            {
                                _loc_5 = UIGlobel.pointsDic["bag"];
                            }
                            else
                            {
                                _loc_5 = UIGlobel.pointsDic[_loc_7[0]];
                            }
                            _loc_5 = _loc_5 == null ? (new Point(100, 100)) : (_loc_5);
                            ModelProxy.change("plugin", "change", {target:_loc_7[0], point:_loc_5, data:_loc_7[1], top:new Point(600, 120)});
                        }
                        else
                        {
                            if (_loc_7[0] == "xunbao" || _loc_7[0] == "juezhan" || _loc_7[0] == "instance")
                            {
                                _loc_5 = UIGlobel.pointsDic[_loc_7[0]];
                            }
                            else
                            {
                                _loc_5 = UIGlobel.pointsDic[_loc_4[1]];
                            }
                            _loc_5 = _loc_5 == null ? (new Point(100, 100)) : (_loc_5);
                            ModelProxy.change("plugin", "change", {target:_loc_4[1], point:_loc_5, top:new Point(600, 120)});
                        }
                    }
                    this._instance.close();
                    break;
                }
                case 2:
                {
                    _loc_6 = _loc_4[1].split("#");
                    ModelProxy.change("data", "taskfindpath", _loc_6[3]);
                    this._instance.close();
                    PluginManager.instance.sendToServer("activity", CMDActivity.Trans, {x:_loc_6[1], y:_loc_6[2], tostageid:_loc_6[0], free:1});
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function isOpenActive(param1:String) : Boolean
        {
            var _loc_2:* = param1.split("-");
            var _loc_3:* = (_loc_2[0] as String).split("#");
            var _loc_4:* = (_loc_2[1] as String).split("#");
            var _loc_5:* = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            var _loc_6:* = new Date(_loc_5 * 1000);
            var _loc_7:* = new Date(_loc_6.fullYear, _loc_6.month, _loc_6.date, int(_loc_3[0]), int(_loc_3[1]), 0, 0);
            var _loc_8:* = new Date(_loc_6.fullYear, _loc_6.month, _loc_6.date, int(_loc_4[0]), int(_loc_4[1]), 0, 0);
            if (_loc_7.time > _loc_8.time)
            {
                _loc_8.time = _loc_8.time + 86400000;
            }
            if (_loc_5 >= _loc_7.time / 1000 && _loc_5 <= _loc_8.time / 1000)
            {
                return true;
            }
            return false;
        }// end function

        protected function showTips(event:MouseEvent) : void
        {
            var _loc_5:String = null;
            var _loc_2:* = event.currentTarget as Label;
            var _loc_3:int = 0;
            while (_loc_3 < this.tasklistArr.length)
            {
                
                if (this.tasklistArr[_loc_3].name == _loc_2.text)
                {
                    _loc_5 = this.tasklistArr[_loc_3].desc;
                    break;
                }
                _loc_3++;
            }
            var _loc_4:* = _loc_2.dataSource;
            ModelProxy.change("tips", "normal", "<font color = \'#01B0F1\'>" + _loc_5 + "</font>");
            _loc_2.addEventListener(MouseEvent.MOUSE_OUT, this.closeTips);
            return;
        }// end function

        protected function closeTips(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as Label;
            var _loc_3:* = _loc_2.dataSource;
            ModelProxy.change("tips", "state", "close");
            _loc_2.removeEventListener(MouseEvent.MOUSE_OUT, this.closeTips);
            return;
        }// end function

        public function completeAct(param1:Object) : void
        {
            var _loc_2:String = null;
            for (_loc_2 in param1)
            {
                
                this._curData[_loc_2] = param1[_loc_2];
            }
            this.showMsg(this._curData);
            return;
        }// end function

        public function getReward(param1:Object) : void
        {
            this._curData[param1[0]] = param1[1];
            var _loc_2:* = list_goods.getCell((param1[0] - 1)).getChildByName("list_item") as List;
            ListFly.go(_loc_2);
            this.showMsg(this._curData);
            return;
        }// end function

        public function qiandao() : Boolean
        {
            var _loc_1:* = UserData.getOnlineData("qd");
            var _loc_2:* = _loc_1.split("#");
            var _loc_3:* = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            var _loc_4:* = new Date(_loc_3 * 1000);
            return int(_loc_2[(_loc_4.date - 1)]) > 0;
        }// end function

    }
}
